package hw6;

import java.util.*;

/**
 * ArrayList based Stack
 * 
 * @author RJ
 *
 * @param <E>
 */
public class ArrayListStack<E> {

	/**
	 * ArrayList that stack is built on
	 */
	private ArrayList<E> stack;
	
	/**
	 * Constructs empty ArrayList
	 */
	public ArrayListStack() {
		stack = new ArrayList<E>();
	}
	
	/**
	 * Adds data to the end of ArrayList
	 * @param data
	 */
	public void push(E data) {
		stack.add(data);
	}
	
	/**
	 * Pops element at the end of ArrayList
	 * @return
	 * @throws EmptyStackException
	 */
	public E pop() throws EmptyStackException {
		if (stack.isEmpty()) {
			throw new EmptyStackException();
		}
		return stack.remove(stack.size()-1);
	}
	
	/**
	 * Checks if ArrayList is empty
	 * @return
	 */
	public boolean empty() {
		return (stack.isEmpty());
	}
	
	/**
	 * Peeks at element at the end of ArrayList
	 * @return
	 * @throws EmptyStackException
	 */
	public E peek() throws EmptyStackException {
		if (stack.isEmpty()) {
			throw new EmptyStackException();
		}
		return stack.get(stack.size()-1);
	}
	
}
